package com.bwie.controller;


import com.bwie.bean.City;
import com.bwie.bean.Shoot;
import com.bwie.bean.Users;
import com.bwie.service.ShootService;
import com.bwie.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ShootController {

    @Resource
    private ShootService shootService;

    @RequestMapping("list.do")
    public String list(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            String sname,
            String sta,
            String ends,
            Model model){

        Map map = new HashMap();
        map.put("sname",sname);
        map.put("sta",sta);
        map.put("ends",ends);

        PageHelper.startPage(pageNum,3);
        List list=shootService.list(map);
        PageInfo pageInfo = new PageInfo(list);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }


    @RequestMapping("dels.do")
    @ResponseBody
    public Object dels(String sids){
       int i= shootService.dels(sids);
        return i;
    }


    @RequestMapping("addto.do")
    public String addto(){
        return "add";
    }

    @RequestMapping("listshoot.do")
    @ResponseBody
    public Object listshoot(){
       List<Shoot> list= shootService.listshoot();
       return list;
    }

    @RequestMapping("listcity.do")
    @ResponseBody
    public Object listcity(Integer pid){
        List<City> list=shootService.listcity(pid);
        return list;
    }

    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Users users,Integer[] sids){

        int i=shootService.add(users,sids);
        return i;
    }

    @RequestMapping("addps.do")
    public String  addps(){
        return "addps";
    }


    @RequestMapping("addpsto.do")
    @ResponseBody
    public Object addps(Shoot shoot,
                        @RequestParam("photo") MultipartFile photo, HttpSession session){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            shoot.setSpic(upload);
        }
        int i=shootService.addps(shoot);
        return i;
    }



    @RequestMapping("updato.do")
    public String updato(){
        return "upda";
    }

    @RequestMapping("updapsto.do")
    public String updapsto(){
        return "updaps";
    }

    @RequestMapping("updaps.do")
    @ResponseBody
    public Object updaps(Shoot shoot,
                        @RequestParam("photo") MultipartFile photo, HttpSession session,
                         Integer[]uids
                         ){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            shoot.setSpic(upload);
        }
        int i=shootService.updaps(shoot,uids);
        return i;
    }

    @RequestMapping("huips.do")
    @ResponseBody
    public Object huips(Integer sid){
        Shoot shoot=shootService.huips(sid);
        return shoot;
    }


    @RequestMapping("listuser.do")
    @ResponseBody
    public Object listusers(){
        List<Users> list=shootService.listusers();
        return list;
    }



}
























